package com.happy.zebra.im.utils;

import io.rong.RongCloud;
import io.rong.methods.user.User;
import io.rong.models.Result;
import io.rong.models.group.GroupMember;
import io.rong.models.group.GroupModel;
import io.rong.models.response.GroupUser;
import io.rong.models.response.GroupUserQueryResult;
import io.rong.models.response.TokenResult;
import io.rong.models.response.UserGroupQueryResult;
import io.rong.models.user.UserModel;
import lombok.SneakyThrows;

import java.util.List;

public class ImUtil {

	private static final String appKey = "uwd1c0sxukfp1";

	private static final String appSecret = "MUlm2HF7ZW";

	private static RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret);

	/**
	 * API 文档: http://www.rongcloud.cn/docs/server_sdk_api/user/user.html#register
	 * <p>
	 * 注册用户，生成用户在融云的唯一身份标识 Token
	 */
	public static String getToken(String userId, String username, String portrait) throws Exception {

		User user = rongCloud.user;
		UserModel userModel = new UserModel()
				.setId(userId)
				.setName(username)
				.setPortrait(portrait);

		TokenResult result = user.register(userModel);

		return result.getToken();
	}


	public static GroupModel[] getUserGroups(String userId) throws Exception {

		UserModel user = new UserModel().setId(userId);

		UserGroupQueryResult userGroupResult = rongCloud.user.getGroups(user);

		System.out.println("getGroups:  " + userGroupResult.toString());

		return userGroupResult.getGroups();
	}

	/**
	 * 创建群组
	 *
	 * @param userIds
	 * @param groupName
	 * @return boolean
	 * @author nianhua.jiang
	 * @date 2021/4/9 10:39
	 **/
	public static boolean createGroup(List<String> userIds, String groupName, String groupId) throws Exception {

		GroupMember[] members = getGroupMembers(userIds);

		GroupModel group = new GroupModel()
				.setId(groupId)
				.setMembers(members)
				.setName(groupName);

		Result result = rongCloud.group.create(group);

		System.out.println("group create result:  " + result.toString());

		return true;

	}

	/**
	 * 查询群成员
	 */
	public static List<GroupUser> groupQueryUser(String groupId) throws Exception {

		GroupModel group = new GroupModel().setId(groupId);

		GroupUserQueryResult result = rongCloud.group.get(group);

		return result.getMembers();

	}

	/**
	 * 退出群组方法测试（将用户从群中移除，不再接收该群组的消息.）
	 */
	public static boolean groupQuit(List<String> userIds, String groupId, String groupName) throws Exception {

		GroupMember[] members = getGroupMembers(userIds);

		GroupModel group = new GroupModel()
				.setId(groupId)
				.setMembers(members)
				.setName(groupName);

		Result result = rongCloud.group.quit(group);

		System.out.println("quit:  " + result.toString());

		return "200".equals(result.getCode().toString());

	}

	/**
	 * 解散群组
	 */
	public static boolean groupDismissResult(String groupId, List<String> userIds) throws Exception {

		GroupMember[] members = getGroupMembers(userIds);

		GroupModel group = new GroupModel()
				.setId(groupId)
				.setMembers(members);

		Result result = rongCloud.group.dismiss(group);

		System.out.println("groupDismissResult:  " + result.toString());

		return "200".equals(result.getCode().toString());
	}

	/**
	 * 将用户加入指定群组，用户将可以收到该群的消息，同一用户最多可加入 500 个群，每个群最大至 3000 人。
	 */
	public static void groupJoin(String groupId, String groupName, List<String> userIds) throws Exception {

		GroupMember[] members = getGroupMembers(userIds);

		GroupModel group = new GroupModel()
				.setId(groupId)
				.setMembers(members)
				.setName(groupName);

		Result result = rongCloud.group.join(group);
		System.out.println("join:  " + result.toString());

	}

	/**
	 * 退出群组方法测试（将用户从群中移除，不再接收该群组的消息.）
	 */
	public static void groupQuit(String groupId, String groupName, List<String> userIds) throws Exception {

		GroupMember[] members = getGroupMembers(userIds);

		GroupModel group = new GroupModel()
				.setId(groupId)
				.setMembers(members)
				.setName(groupName);
		Result result = (Result) rongCloud.group.quit(group);
		System.out.println("quit:  " + result.toString());

	}

	private static GroupMember[] getGroupMembers(List<String> userIds) {

		GroupMember[] members = new GroupMember[userIds.size()];

		for (int i = 0; i < userIds.size(); i++) {
			members[i] = new GroupMember().setId(userIds.get(i));
		}

		return members;
	}

}
